<template
  ><div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 第一行: 搜索框 gutter 设置单元格间隔 -->
      <el-row :gutter="20">
        <!-- 第一列 span 设置列的 宽度比例 (每行的总span为24) -->
        <el-col :span="8">
          <el-input
            ref="searchInput"
            placeholder="请输入内容"
            clearable
            v-model="queryData.query"
            @clear="getOrderList"
          >
            <el-button @click="search" slot="append" icon="el-icon-search">
            </el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表数据 -->
      <el-table :data="orderlist" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column
          label="订单编号"
          prop="order_number"
          width="350"
        ></el-table-column>
        <el-table-column
          label="订单价格"
          prop="order_price"
          width="100"
        ></el-table-column>
        <el-table-column label="是否付款" prop="pay_status" width="100">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="100">
          <template slot-scope="scope">
            <template>
              {{ scope.row.is_send }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="270">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="isEditAdressShow = true"
            ></el-button>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-location"
              @click="showProressDialog"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="pageChange"
        :current-page="queryData.pagenum"
        :page-size="queryData.pagesize"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 修改地址的对话框 -->
      <el-dialog
        v-dialogDrag
        title="修改地址"
        :visible.sync="isEditAdressShow"
        width="50%"
      >
        <el-form
          :model="addressForm"
          :rules="addressFormRules"
          ref="addressFormRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              :props="{ expandTrigger: 'hover' }"
              :options="cityList"
              v-model="addressForm.address1"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <!-- 尾部区域 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="isEditAdressShow = false">取 消</el-button>
          <el-button type="primary" @click="isEditAdressShow = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 展示物流进度的对话框 -->
      <el-dialog
        v-dialogDrag
        title="物流进度"
        :visible.sync="isProgressVisible"
        width="50%"
      >
        <!-- 时间线 -->
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.time"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import cityList from './citydata.js'

export default {
  data() {
    return {
      progressInfo: [],
      // 直接获取  导入城市数组
      cityList,
      // 选中要修改城市的下拉框id
      selectedCityIds: [],
      // 修改 地址验证规则
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      addressForm: {
        address1: [],
        address2: ''
      },
      // 物流进度对话框  开关
      isProgressVisible: false,
      // 修改地址对话框  开关
      isEditAdressShow: false,
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      orderlist: [],
      total: 0 // 总页码
    }
  },
  // 相当于 vur 入口函数,当配置对象成员 挂在到实例后执行
  created() {
    this.getOrderList()
  },
  methods: {
    // 4.点击页码事件
    pageChange(newPage) {
      // 修改当前页
      this.queryData.pagenum = newPage
      // 重新请求 当前页 数据
      this.getOrderList()
    },
    // 3.页容量改变事件处理函数
    handleSizeChange(newSize) {},
    // 2.搜索
    search() {
      this.getOrderList()
    },
    // 1.获取订单数据列表
    async getOrderList() {
      const { data: resData } = await this.$http.get('orders', {
        params: this.queryData
      })
      //    如果失败
      if (resData.meta.status != 200) {
        return this.$message.error(resData.meta.msg)
      }
      // 如果成功
      this.$message.success(resData.meta.msg)
      this.total = resData.data.total
      this.orderlist = resData.data.goods
    },
    async showProressDialog() {
      //   // 显示物流对话框
      this.isProgressVisible = true
      // 请求物流数据
      const { data: resData } = await this.$http.get(
        '/kuaidi/804909574412544580'
      )
      console.log(('物流结果:', resData))
    }
  }
}
</script>
<style lang="less" scoped></style>
